﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Common
{
    public partial class PopupContainerEdit : UserControl
    {
        public UIElement popupControl
        {
            get
            {
                return border.Child;
            }
            set
            {
                border.Child = value;
            }
        }

        public string Text
        {
            get
            {
                return txtBox.Text;
            }
            set
            {
                txtBox.Text = value;
            }
        }

        public string HiddenText
        {
            get
            {
                return (string)txtBox.Tag;
            }
            set
            {
                txtBox.Tag = value;
            }
        }


        public System.Windows.Controls.Primitives.Popup Popup
        {
            get
            {
                return this.popup;
            }
        }

        private System.Windows.Controls.Primitives.Popup popup = new System.Windows.Controls.Primitives.Popup();
        private Border border;

        public PopupContainerEdit()
        {
            InitializeComponent();

            StackPanel stpnl = new StackPanel();
            stpnl.Orientation = Orientation.Vertical;

            border = new Border();
            stpnl.Children.Add(border);

            Grid grid = new Grid();
            Button btn = new Button();
            btn.Style = ((System.Windows.Style)Application.Current.Resources["style_button"]);
            btn.Content = "x";
            btn.Width = 20;
            btn.Height = 25;
            btn.HorizontalAlignment = HorizontalAlignment.Right;
            btn.Click += new RoutedEventHandler(btn_Click);
            grid.Children.Add(btn);
            stpnl.Children.Add(grid);

            popup.Child = stpnl;
        }

        void btn_Click(object sender, RoutedEventArgs e)
        {
            if (Popup.IsOpen)
                Popup.IsOpen = false;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // Set where the popup will show up on the screen.
            GeneralTransform gt = txtBox.TransformToVisual((UIElement)GetTopUIElement());
            Point p = gt.Transform(new Point(0, 0));

            popup.VerticalOffset = p.Y + txtBox.Height; 
            popup.HorizontalOffset = p.X;

            // Open the popup.
            if (!Popup.IsOpen)
                Popup.IsOpen = true;
           
        }


        private DependencyObject GetTopUIElement()
        {
            DependencyObject parent = VisualTreeHelper.GetParent(this);

            while (parent != null)
            {
                DependencyObject tempParent = VisualTreeHelper.GetParent(parent);

                if (tempParent == null)
                    break;

                parent = tempParent;
            }

            return parent;
        }

        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (Popup.IsOpen)
                Popup.IsOpen = false;
        }
    }
}
